<?php
class Com_contact extends CI_Controller{
    public $rear ='';
    function  __construct(){
        parent::__construct(); 
        if($this->session->userdata('ngonngu') && $this->session->userdata('ngonngu')=='_en') {
            $this->rear = '_en';
            $this->lang->load('atta','english');
        }else{
            $this->rear = '';
            $this->lang->load('atta','vietnam');
        }  
        $this->load->library('form_validation');
        $this->setting =$this->common_model->getone('setting',array('id'=>1));
    }
    function index(){
        $thongbao ='';
        $this->load->library('form_validation');
        $name =$this->lang->line('name_regis');
        $email =  $this->lang->line('email_regis');
        $phone =  $this->lang->line('phone_regis');
        $add =  $this->lang->line('add_regis');
        $content =  $this->lang->line('content_regis');
        $this->form_validation->set_rules('name',$name,'trim|required|min_length[5]|max_length[100]|xss_clean');
        $this->form_validation->set_rules('phone',$phone, 'trim|numeric|min_length[9]|max_length[15]|required|xss_clean');
        $this->form_validation->set_rules('address',$add, 'trim|required|xss_clean');
        $this->form_validation->set_rules('email',$email, 'trim|required|valid_email|xss_clean');
        $this->form_validation->set_rules('content',$content, 'trim|xss_clean');
                //setcookie("lienhe","OK", time()-3600);
        if(empty($_COOKIE["lienhe"])){
            if ($this->form_validation->run() == FALSE){
               
            }
            else{
			//print_r($_POST);
            $data = $this->security->xss_clean($_POST);
            $birthday = $this->input->post('day').'/'.$this->input->post('month').'/'.$this->input->post('year');
            $arr = array(
                'email' => $this->input->post('email'),
                'phone' => $this->input->post('phone'),
                'address' => $this->input->post('address'),
                'name' => $this->input->post('name'),
                'content' => $this->input->post('content'),
                'ip'=>$this->input->ip_address()
            );
            $thongbao= '<p>Chúng tôi đã nhận được thư liên hệ</p><p>Chúng tôi sẽ liên lạc lại với bạn trong thời gian sớm nhất!</p>';
            setcookie("lienhe",$thongbao, time()+300);            
            $this->db->insert('contact', $arr); 
            }
        }else{
            $thongbao = '<p>Bạn đã gửi thư liên hệ cách đây không lâu</p>';
            setcookie("lienhe",$thongbao, time()+3600);
        }
            $catserch = $this->lang->line('contact1');
            $data['content'] = $this->load->view('com_contact/form_contact',array('thongbao'=>$thongbao,'rear'=>$this->rear),true);
            $data['title'] = $this->setting->title;
            $data['metades'] = $this->setting->metades;
            $data['metakey'] = $this->setting->metakey;
            $this->load->view('index',$data);    
    }
}